package com.jcl.recruit.user.mapper;

import com.jcl.recruit.user.model.User;
import com.jcl.recruit.user.vo.UserSearchEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author 刘明新
 * @date 2018/8/2 下午7:11
 */
public interface UserMapper {

    @Select({
            "<script>",
            "select * from user ",
            "<where>",
            "<if test=\"null != corpCode\"> corp_code = #{corpCode} </if>",
            "<if test=\"null != ids\"> and id in ",
            "<foreach collection='ids' item='id' open='(' close=')' separator=',' >",
            " #{id}",
            "</foreach>",
            " </if>",
            "</where>",
            "</script>"
    })
    List<User> findUserListByIds(@Param("corpCode") String corpCode, @Param("ids") List<Integer> ids);
    @Select({
            "<script>",
            "select * from user ",
            "<where>",
            "<if test=\"null != ids\"> id in ",
            "<foreach collection='ids' item='id' open='(' close=')' separator=',' >",
            " #{id}",
            "</foreach>",
            " </if>",
            "</where>",
            "</script>"
    })
    List<User> findUserList(@Param("ids") List<Integer> ids);

    @Select({
            "<script>",
            "select * from user ",
            "<where>",
            "<if test=\"null != corpCode\"> corp_code = #{corpCode} </if>",
            "<if test=\"null != status\"> and status = #{status} </if>",
            "</where>",
            "order by create_time desc",
            "</script>"
    })
    List<User> findListByStatus(@Param("corpCode") String corpCode, @Param("status") Integer status);

    @Select({
            "<script>",
            "select * from user ",
            "<where>",
            "<if test=\"null != entity.corpCode\"> corp_code = #{entity.corpCode} </if>",
            "<if test=\"null != entity.keyword and '' != entity.keyword \"> ",
            "and (realname like '%${entity.keyword}%' or username like '%${entity.keyword}%' or email like '%${entity.keyword}%' ) " ,
            "</if>",
            "<if test=\"null != entity.status\"> and status = #{entity.status} </if>",
            "</where>",
            "order by create_time desc",
            "</script>"
    })
    List<User> findListByEntity(@Param("entity") UserSearchEntity entity);

    @Select({
            "<script>",
            "select * from user ",
            "<where>",
            "<if test=\"null != corpCode\"> corp_code = #{corpCode} </if>",
            "</where>",
            "order by create_time desc",
            "</script>"
    })
    List<User> findAll(@Param("corpCode") String corpCode);

    @Select({
            "<script>",
            "select * from user ",
            "<where>",
            "<if test=\"null != corpCodes\"> corp_code in ",
            "<foreach collection='corpCodes' item='corpCode' open='(' close=')' separator=',' >",
            " #{corpCode}",
            "</foreach>",
            " </if>",
            " and roles like '%0%' ",
            "</where>",
            "</script>"
    })
    List<User> findAdminUserListByCorp(@Param("corpCodes") List<String> corpCodes);

    @Select({
            "select * from user where corp_code = #{corpCode} and roles like '%0%' "
    })
    User findAdminUserByCorp(@Param("corpCode") String corpCode);

    @Select({
            "select * from user where username = #{username} and password = #{password}"
    })
    Map<String,Object> selectPrimaryKey(@Param("username") String username, @Param("password") String password);

    @Select({
            "select max(id) as maxId from user "
    })
    Map<String,Object> selectMaxIdKey();

    @Select({
            "<script>",
            "select count(id) as members from user ",
            "<where>",
            "<if test=\"null != corpCode\"> corp_code = #{corpCode} </if>",
            "</where>",
            "</script>"
    })
    Map<String,Object> countMember(@Param("corpCode") String corpCode);

    @Select({
            "select * from user where token = #{token}"
    })
    Map<String,Object> selectPrimaryKeyByToken(@Param("token") String token);

    @Select({
            "select * from user where id = #{id}"
    })
    Map<String,Object> selectPrimaryKeyById(@Param("id") Integer id);

    @Select({
            "select * from user where id = #{id}"
    })
    User findOneById(@Param("id") Integer id);

    @Select({
            "select * from user where corp_code = #{corpCode} and id = #{id}"
    })
    User findOne(@Param("corpCode") String corpCode, @Param("id") Integer id);

    @Select({
            "select * from user where username = #{username}"
    })
    Map<String,Object> selectPrimayByUsername(@Param("username") String username);

    @Select({
            "<script>",
            "select * from user",
            "<where>",
            "<if test=\"realname != null\"> and realname like '%${realname}%' </if>",
            "<if test=\"isHr != null\"> and is_hr = #{isHr} </if>",
            "<if test=\"corpCode != null\">and corp_ode = #{corpCode} </if>",
            "<if test=\"departId != null\">and depart_id = #{departId} </if>",
            "</where>",
            " order by create_time desc",
            "</script>"
    })
    List<Map<String,Object>> selectUsersByCondition(@Param("realname") String realname,
                                                    @Param("isHr") Integer isHr,
                                                    @Param("corpCode") String corpCode,
                                                    @Param("departId") Integer departId);

    @InsertProvider(type=UserSqlProvider.class, method="insertSelective")
    @Options(useGeneratedKeys=true,keyProperty="id")
    int insertSelective(User record);

    @UpdateProvider(type=UserSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(User record);

    @Update({
            "update user set photo_id = #{photoId} where id = #{id}"
    })
    int updateUserHead(@Param("id") Integer id, @Param("photoId") String photoId);

    @Select({
            "select id from user where email = #{email}"
    })
    Integer selectPrimaryIdByEmail(@Param("email") String email);
}
